package com.getpebble.android.onboarding.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.common.model.FrameworkState.EventType;
import com.getpebble.android.common.model.LockerApp;
import com.getpebble.android.common.model.PblDevice;
import com.getpebble.android.framework.FrameworkEventReceiver;
import com.getpebble.android.framework.FrameworkEventReceiver.IFrameworkEventListener;
import com.getpebble.android.framework.PblFrameworkInterface;
import com.getpebble.android.framework.endpoint.InstallAppEndpoint.AppInstallResult;
import com.getpebble.android.onboarding.activity.OnboardingActivity;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.util.ArrayList;

public class AppInstallFragment extends PblBaseFragment {
    public static final String TAG = AppInstallFragment.class.getSimpleName();
    private int mAppInstallIndex = 0;
    private int mAppUninstallIndex = 0;
    private int mAppsInstallFailed = 0;
    private int mAppsInstalled = 0;
    private ArrayList<LockerApp> mAppsToLoad = new ArrayList();
    private PblFrameworkInterface mFrameworkInterface = null;
    private IFrameworkEventListener mInstallEventListener = new IFrameworkEventListener() {
        public void onFrameworkStateChanged(FrameworkState newState) {
            if (newState != null && newState.getLastEvent() != null) {
                switch (AnonymousClass2.$SwitchMap$com$getpebble$android$common$model$FrameworkState$EventType[newState.getLastEvent().ordinal()]) {
                    case SMTPMessage.RETURN_FULL /*1*/:
                        if (AppInstallResult.fromValue(newState.getAppInstallResult()).equals(AppInstallResult.SUCCESS)) {
                            AppInstallFragment.access$004(AppInstallFragment.this);
                        } else {
                            AppInstallFragment.access$104(AppInstallFragment.this);
                        }
                        AppInstallFragment.this.mIsInstallInProgress = false;
                        AppInstallFragment.this.installApp();
                        return;
                    case SMTPMessage.RETURN_HDRS /*2*/:
                        AppInstallFragment.this.mIsInstallInProgress = false;
                        AppInstallFragment.this.unInstallApp();
                        return;
                    case ListInfo.INDETERMINATE /*3*/:
                        if (AppInstallFragment.this.isResumed()) {
                            AppInstallFragment.this.mProgressBar.setProgress(newState.getAppInstallProgress());
                            return;
                        }
                        return;
                    default:
                        Trace.info(AppInstallFragment.TAG, "Dropping event: " + newState.getLastEvent());
                        return;
                }
            }
        }
    };
    private TextView mInstallStatus;
    private TextView mInstallingApp;
    private boolean mIsInstallInProgress = false;
    private ProgressBar mProgressBar;
    private ArrayList<String> mUuidsToUnload = new ArrayList();

    static /* synthetic */ class AnonymousClass2 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$common$model$FrameworkState$EventType = new int[EventType.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$common$model$FrameworkState$EventType[EventType.APP_INSTALL_COMPLETE.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$model$FrameworkState$EventType[EventType.APP_REMOVE_COMPLETE.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$model$FrameworkState$EventType[EventType.APP_INSTALL_PROGRESS_CHANGED.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
        }
    }

    static /* synthetic */ int access$004(AppInstallFragment x0) {
        int i = x0.mAppsInstalled + 1;
        x0.mAppsInstalled = i;
        return i;
    }

    static /* synthetic */ int access$104(AppInstallFragment x0) {
        int i = x0.mAppsInstallFailed + 1;
        x0.mAppsInstallFailed = i;
        return i;
    }

    public int getLayoutId() {
        return R.layout.fragment_install_apps;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mProgressBar = (ProgressBar) container.findViewById(R.id.onboarding_app_install_progress);
        this.mProgressBar.setMax(100);
        this.mInstallingApp = (TextView) container.findViewById(R.id.onboarding_app_install_app);
        this.mInstallStatus = (TextView) container.findViewById(R.id.onboarding_app_install_status);
        this.mFrameworkInterface = getFrameworkInterface();
        registerFrameworkStateEventListener(this.mInstallEventListener);
    }

    public void onPause() {
        super.onPause();
    }

    public void onResume() {
        super.onResume();
        handleArguments();
        unInstallApp();
    }

    public void onDestroy() {
        super.onDestroy();
        unregisterFrameworkStateEventListener(this.mInstallEventListener);
    }

    private void handleArguments() {
        Bundle args = getArguments();
        if (args != null && args.containsKey("extra_load_app_list")) {
            this.mAppsToLoad = args.getParcelableArrayList("extra_load_app_list");
        }
        if (args != null && args.containsKey("extra_unload_uuid_list")) {
            this.mUuidsToUnload = args.getStringArrayList("extra_unload_uuid_list");
        }
    }

    private void unInstallApp() {
        if (this.mIsInstallInProgress) {
            Trace.debug(TAG, "Install already in progress; dropping uninstall request");
        } else if (this.mAppUninstallIndex >= this.mUuidsToUnload.size()) {
            installApp();
        } else {
            this.mIsInstallInProgress = true;
            String uuid = (String) this.mUuidsToUnload.get(this.mAppUninstallIndex);
            this.mAppUninstallIndex++;
            this.mFrameworkInterface.uninstallApp(getConnectedDevice(), uuid);
            if (isResumed()) {
                this.mInstallStatus.setText(String.format(getString(R.string.text_multi_app_uninstalling), new Object[]{Integer.valueOf(this.mAppUninstallIndex), Integer.valueOf(this.mUuidsToUnload.size())}));
                this.mProgressBar.setProgress(0);
                return;
            }
            Trace.debug(TAG, String.format("Started uninstall of %s but fragment is not resumed; will update UI in next onResume", new Object[]{uuid}));
        }
    }

    private void installApp() {
        if (this.mIsInstallInProgress) {
            Trace.debug(TAG, "Install already in progress; dropping install request");
        } else if (this.mAppInstallIndex < this.mAppsToLoad.size()) {
            this.mIsInstallInProgress = true;
            LockerApp appToLoad = (LockerApp) this.mAppsToLoad.get(this.mAppInstallIndex);
            Trace.debug(TAG, "Installing app: " + appToLoad.getAppName());
            this.mAppInstallIndex++;
            this.mFrameworkInterface.installApp(getConnectedDevice(), Uri.parse(appToLoad.getPbwFile()), false, true);
            if (isResumed()) {
                this.mInstallStatus.setText(String.format(getString(R.string.text_multi_app_installing), new Object[]{Integer.valueOf(this.mAppInstallIndex), Integer.valueOf(this.mAppsToLoad.size())}));
                this.mInstallingApp.setText(appToLoad.getAppName());
                this.mProgressBar.setProgress(0);
                return;
            }
            Trace.debug(TAG, String.format("Started install of %s app in background; fragment UI will be updated in next onResume", new Object[]{appToLoad.getAppName()}));
        } else if (isResumed()) {
            goToNextFragment();
        } else {
            Trace.warning(TAG, "Finished app install but fragment is not resumed; will advance in next onResume");
        }
    }

    void goToNextFragment() {
        Activity activity = getActivity();
        if (activity != null && (activity instanceof OnboardingActivity)) {
            OnboardingActivity onboardingActivity = (OnboardingActivity) activity;
            Fragment nextFragment = new AppInstallCompleteFragment();
            Bundle args = new Bundle();
            args.putInt("extra_num_apps_installed", this.mAppsInstalled);
            args.putInt("extra_num_apps_install_failed", this.mAppsInstallFailed);
            nextFragment.setArguments(args);
            onboardingActivity.switchScreenFragment(nextFragment);
        }
    }

    void registerFrameworkStateEventListener(IFrameworkEventListener eventListener) {
        FrameworkEventReceiver.registerFrameworkStateEventListener(eventListener);
    }

    void unregisterFrameworkStateEventListener(IFrameworkEventListener eventListener) {
        FrameworkEventReceiver.unregisterFrameworkStateEventListener(eventListener);
    }

    PblFrameworkInterface getFrameworkInterface() {
        return PebbleApplication.getFrameworkInterface();
    }

    PblDevice getConnectedDevice() {
        return PebbleApplication.getConnectedDevice();
    }
}
